package com.mall507.fivepeaks.activity.community;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.core.ImageFrame.BaseImage;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.util.AndroidUtil;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.ShopItem;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.Util;
import com.mall507.fivepeaks.widget.Banner.BGABanner;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 协会详情
 * @date 2019/8/8
 */
@Route(path = "/community/CommunityAssociationInfoRUI")
public class CommunityAssociationInfoRUI extends AbsRecyclerViewUI<ShopItem> {

    public final int ITEM_TYPE_DYNAMIC = 0xff04;//动态
    public final int ITEM_TYPE_ACTIVITY = 0xff05;//活动

    View headView;
    int imageSize = 0;

    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_community_association_info;
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("详情");
        showLoding();
        headView = getLayoutInflater().inflate(R.layout.head_rv_community_association_info, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        headView.setLayoutParams(layoutParams);
        getAdapter().setHeaderView(headView);
        getComment();
    }

    @Override
    protected String setHttpPath() {
        return "Association/getAssociationActivity";
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<ShopItem> handleHttpData(JSONObject response) {
        List<ShopItem> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONObject jsonObject = response.optJSONObject("data");
            JSONArray activity_list = jsonObject.optJSONArray("activity_list");
            for (int i = 0; i < activity_list.length(); i++) {
                JSONObject object = activity_list.optJSONObject(i);
                ShopItem model = new ShopItem();
                model.setNid(object.optString("id"));
                model.setName(object.optString("title")); //活动标题
                JSONArray imgs = object.optJSONArray("imgs");
                if (imgs != null && imgs.length()>0){
                    imageSize = imgs.length();
                    model.setImgSize(String.valueOf(imageSize));
                    for (int j = 0;j<imgs.length();j++){
                        switch (j){
                            case 0:
                                model.setIcon1(imgs.optString(j));
                                break;
                            case 1:
                                model.setIcon2(imgs.optString(j));
                                break;
                            case 2:
                                model.setIcon3(imgs.optString(j));
                                break;
                        }
                    }
                }else {
                    imageSize = 0;
                    model.setImgSize(String.valueOf(imageSize));
                }

                model.setCoverImg(object.optString("cover_img")); //活动图片
                model.setActivity_wx_qr(object.optString("activity_wx_qr")); //活动二维码
                model.setNumber(object.optString("activity_number")); //活动人数
                model.setTime(object.optString("activity_time")); //活动时间
                model.setAddress(object.optString("activity_address")); //活动地址
                model.setUserHead(object.optString("head_img")); //活动发布者头像
                model.setUserName(object.optString("nickname"));
                model.setUserTime(object.optString("ctime"));
                model.setBrowsingNum(object.optString("views"));
                model.setIs_activity(object.optString("is_activity"));
                model.setCollection_count(object.optString("collection_count"));
                list.add(model);
            }
        }
        dismissLoding();
        return list;
    }



    @Override
    protected int setItemViewType(int position) {
        String type = getAdapter().getDatalist().get(position).getIs_activity();
        if (type.equals("0")) {
            return ITEM_TYPE_DYNAMIC;
        } else if (type.equals("1")) {
            return ITEM_TYPE_ACTIVITY;
        }
        return ITEM_TYPE_DYNAMIC;
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        if (viewType == ITEM_TYPE_DYNAMIC) {
            return R.layout.item_rv_community_association_dynamic_info;
        } else if (viewType == ITEM_TYPE_ACTIVITY) {
            return R.layout.item_rv_community_association_activity_info;
        }
        return R.layout.item_rv_community_association_dynamic_info;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, ShopItem model) {
        ((CircleImageView) holder.getView(R.id.head)).setImageURI(model.getUserHead());
        holder.setText(R.id.name, model.getName());
        holder.setText(R.id.nice, model.getUserName() + "  " + model.getUserTime());
//        holder.setText(R.id.time, model.getUserTime());
        holder.setText(R.id.pageview, model.getBrowsingNum() + "人浏览");
        int size = imageSize;
        switch (getAdapter().getItemViewType(position)) {
            case ITEM_TYPE_DYNAMIC:
                bindDynamic(holder, position, model,size);
                break;
            case ITEM_TYPE_ACTIVITY:
                bindActivity(holder, position, model);
                break;
        }
    }

    public void bindDynamic(RecyclerViewHolder holder, int position, final ShopItem model,int imageSize) {
        if (model.getImgSize() != null && model.getImgSize().length()>0){
            if (model.getImgSize().equals("1")){
                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
                ((ExpandImageView) holder.getView(R.id.image2)).setVisibility(View.INVISIBLE);
                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.INVISIBLE);
            }else if (model.getImgSize().equals("2")){
                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
                ((ExpandImageView) holder.getView(R.id.image2)).setImageURI(model.getIcon2());
                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.INVISIBLE);
            }else if (model.getImgSize().equals("3")){
                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
                ((ExpandImageView) holder.getView(R.id.image2)).setImageURI(model.getIcon2());
                ((ExpandImageView) holder.getView(R.id.image3)).setImageURI(model.getIcon3());
            }else if (model.getImgSize().equals("0")){
                ((ExpandImageView) holder.getView(R.id.image1)).setVisibility(View.GONE);
                ((ExpandImageView) holder.getView(R.id.image2)).setVisibility(View.GONE);
                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.GONE);
            }
        }
//        if (imageSize>0){
//            if (imageSize == 1){
//                ((ExpandImageView) holder.getView(R.id.image1)).setVisibility(View.VISIBLE);
//                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
//                ((ExpandImageView) holder.getView(R.id.image2)).setVisibility(View.INVISIBLE);
//                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.INVISIBLE);
//            }else if (imageSize == 2){
//                ((ExpandImageView) holder.getView(R.id.image1)).setVisibility(View.VISIBLE);
//                ((ExpandImageView) holder.getView(R.id.image2)).setVisibility(View.VISIBLE);
//                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
//                ((ExpandImageView) holder.getView(R.id.image2)).setImageURI(model.getIcon2());
//                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.INVISIBLE);
//            }else {
//                ((ExpandImageView) holder.getView(R.id.image1)).setVisibility(View.VISIBLE);
//                ((ExpandImageView) holder.getView(R.id.image2)).setVisibility(View.VISIBLE);
//                ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.VISIBLE);
//                ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
//                ((ExpandImageView) holder.getView(R.id.image2)).setImageURI(model.getIcon2());
//                ((ExpandImageView) holder.getView(R.id.image3)).setImageURI(model.getIcon3());
//            }
//        }else {
//            ((ExpandImageView) holder.getView(R.id.image1)).setVisibility(View.GONE);
//            ((ExpandImageView) holder.getView(R.id.image2)).setVisibility(View.GONE);
//            ((ExpandImageView) holder.getView(R.id.image3)).setVisibility(View.GONE);
//        }
                        ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
                        ((ExpandImageView) holder.getView(R.id.image2)).setImageURI(model.getIcon2());
                        ((ExpandImageView) holder.getView(R.id.image3)).setImageURI(model.getIcon3());
                holder.setOnItemClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                ARouter.getInstance()
                        .build("/comn/ComnArticleItemInfoRUI")
                        .withString("type","7")
                        .withString("collection_count",model.getCollection_count())
                        .withString("id", model.getNid())
                        .withString("commentRequestPath","Comment/getAssociationComment")
                        .withString("headRequestPath","Association/getAssociationActivity")
                        .navigation();
            }
        });
    }

    public void bindActivity(RecyclerViewHolder holder, int position, final ShopItem model) {
        ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getCoverImg());
        holder.setText(R.id.child_number, "人数：" + model.getNumber());
        holder.setText(R.id.child_time, "时间：" + model.getTime());
        holder.setText(R.id.child_address, "地点：" + model.getAddress());
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (model.getIs_activity()){
                    case "1":
                        ARouter.getInstance().build("/community/CommunityAssociationInfoActivityRUI")
                                .withString("id", model.getNid())
                                .withString("type","6")
                                .withString("collection_count",model.getCollection_count())
                                .navigation();
                        break;
                    case "0":
                        ARouter.getInstance().build("/community/CommunityAssociationInfoDynamicRUI")
                                .withString("id", model.getNid())
                                .withString("type","7")
                                .withString("collection_count",model.getCollection_count())
                                .navigation();
                        break;
                }
            }
        });
    }

    private void getComment() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        BaseHttp.getInstance().query("Association/getAssociationActivity", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                JSONObject data = response.optJSONObject("data");
                JSONObject association = data.optJSONObject("association");
                getTitleView().setContent(association.optString("name"));
                ((CircleImageView)headView.findViewById(R.id.head_icon)).setImageURI(association.optString("logo")); //协会logo
                ((TextView)headView.findViewById(R.id.head_name)).setText(association.optString("name")); //协会名称
                ((TextView)headView.findViewById(R.id.head_time)).setText("成立时间:"+association.optString("ctime")); //成立时间
                ((TextView)headView.findViewById(R.id.head_content)).setText(association.optString("desc")); //协会描述
            }
        });
    }

    @Override
    protected void onRestart() {
        onRefresh();
        super.onRestart();
    }
}
